<template>
  <view v-if="goodsInfo.goods_name" class="goods-detail_container">
    <!-- 商品轮播图 -->
    <swiper class="swiper-box" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"
      :circular="true">
      <swiper-item v-for="(item, index) in goodsInfo.pics" :key="index">
        <image class="goods-pic" :src="item.pics_big" @click="previewPicture(index)"></image>
      </swiper-item>
    </swiper>

    <!-- 商品详情信息 -->
    <view class="goods-info">
      <!-- 商品价格 -->
      <view class="goods-info_price">￥{{goodsInfo.goods_price}}</view>
      <!-- 商品主要信息 -->
      <view class="goods-info_main">
        <!-- 商品名称 -->
        <view class="goods-name">{{goodsInfo.goods_name}}</view>
        <!-- icon 收藏 -->
        <view class="goods-favorite">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="trans-expense">快递: 免运费</view>
    </view>

    <!-- 富文本 - 商品详情信息 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>

    <!-- 
    商品导航组件 
      fill 控制右侧按钮的样式
      options 左侧按钮的配置项
      buttonGroup 右侧按钮的配置项
      click 左侧按钮的点击事件处理函数
      buttonClick 右侧按钮的点击事件处理函数
    -->

    <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
      @buttonClick="buttonClick" />
  </view>
</template>

<script>
  import cartBadge from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        goods_id: '', // 商品id
        goodsInfo: {}, // 商品详情信息
        options: [{ // 左侧按钮组的配置对象
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{ // 右侧按钮组的配置对象
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    
    mixins: [cartBadge],

    onLoad(option) {
      // console.log(option)
      this.goods_id = option.goods_id
      // 调用请求商品详情数据的方法
      this.getGoodsInfo(this.goods_id)
    },

    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['cartTotal'])
    },

    watch: {
      // 通过形参得到变化后的新值
      cartTotal: {
        handler(newVal) {
          // console.log(newVal)
          const optionsResult = this.options.find(item => item.text === '购物车')
          if (optionsResult) {
            optionsResult.info = newVal
          }
        },
         // immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用
        immediate: true
      }
    },

    methods: {
      ...mapMutations('m_cart', ['addCartCount']),
      // 定义请求商品详情数据的方法
      async getGoodsInfo(id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: id
        })
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题; 将 webp 的后缀名替换为 jpg 的后缀名
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;" ')
          .replace(/webp/g, 'jpg')
        this.goodsInfo = res.message
      },

      // 实现轮播图的预览效果
      previewPicture(index) {
        // 调用 uni.previewImage() 方法预览图片
        uni.previewImage({
          // 预览时，默认显示图片的索引
          current: index,
          // 所有图片 url 地址的数组
          urls: this.goodsInfo.pics.map(x => x.pics_big)
        })
      },

      // 左侧按钮的点击事件处理函数
      onClick(e) {
        // console.log(e)
        if (e.content.text === "购物车") {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },

      // 右侧按钮点击事件处理函数
      buttonClick(e) {
        // console.log(e)
        if (e.content.text === "加入购物车") {
          /**
           * 商品信息对象:
           * { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
           */
          const goodsObj = {
            goods_id: this.goodsInfo.goods_id,
            goods_name: this.goodsInfo.goods_name,
            goods_price: this.goodsInfo.goods_price,
            goods_count: 1,
            goods_small_logo: this.goodsInfo.goods_logo,
            goods_state: this.goodsInfo.goods_state,
          }
          // 通过vuex把商品信息对象存储到购物车中
          this.addCartCount(goodsObj)
        }
      }
    }
  }
</script>

<style lang="scss">
  .goods-detail_container {
    padding-bottom: 50px;

    .swiper-box {
      height: 750rpx;

      .goods-pic {
        width: 100%;
        height: 100%;
      }
    }

    .goods-info {
      padding: 10px 0 10px 10px;

      &_price {
        color: #c00000;
        font-size: 18px;
        margin: 10px 0;
      }

      &_main {
        display: flex;
        justify-content: space-between;

        .goods-name {
          font-size: 13px;
          padding-right: 10px;
        }

        .goods-favorite {
          width: 120px;
          display: flex;
          font-size: 12px;
          color: gray;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          border-left: 1px solid #efefef;
        }
      }

      .trans-expense {
        font-size: 12px;
        color: gray;
        margin-top: 10px;
      }
    }

    .uni-goods-nav {
      width: 100%;
      position: fixed;
      bottom: 0;
      left: 0;
    }
  }
</style>
